package chat.client;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

import chat.ui.GroupChatWindow;
import chat.user.Group;
import chat.user.Message;

public class ClientRequestor implements Runnable {
	
	public ObjectOutputStream oos;
	public boolean GROUP_REQUEST=false;
	public boolean NEW_GROUP_REQUEST=false;
	public boolean AUTH_REQUEST=false;
	private Message m;
	public GroupChatWindow group_chat;
	private String new_group_name;
	public String user_name;
	private Socket s;
	
	public ClientRequestor(Socket socket) throws Exception{
		
		s = socket;
		
		//start();
	}
	
	@Override
	public void run(){
		
		while (true){
			
			try {
				
				/* Old code
				 * for (; ClientMain.getOutMessageCount()>0; ){
				 */
				while (ClientMain.getOutMessageCount()>0){
					
						Message m = ClientMain.getOutMessage();
					
						ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
						
						oos.writeObject(m);
						oos.flush();
						
						ClientMain.removeOutMessage(m);
					}
				} catch (IOException e) {
					
					e.printStackTrace();
				}
			
			
			
		}
		
	}

	
	public boolean RequestGroupStatus(){
		return this.GROUP_REQUEST=true;
	}
	
	public GroupChatWindow getChat(){
		return this.group_chat;
	}
	
	
	public  boolean AuthMessage (){
		
		return AUTH_REQUEST = true;
	}
	
}
